home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / tnos / tnos100s / ttylink.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-30  |  2.8 KB  |  129 lines

  1. /* Internet TTY "link" (keyboard chat) server
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include "global.h"
  7. #include "config.h"
  8. #include "mbuf.h"
  9. #include "socket.h"
  10. #include "telnet.h"
  11. #include "session.h"
  12. #include "proc.h"
  13. #include "tty.h"
  14. #include "mailbox.h"
  15. #include "commands.h"
  16.  
  17. #ifdef ALLSERV
  18. static int Sttylink = -1;    /* Protoype socket for service */
  19. extern int Attended;
  20. static char Tnbanner[] = "Welcome to TTY-Link at %s - The system is %s.\n";
  21. extern char SysopBusy[];
  22.  
  23. int
  24. ttylstart(argc,argv,p)
  25. int argc;
  26. char *argv[];
  27. void *p;
  28. {
  29.     struct sockaddr_in lsocket;
  30.     int s,type;
  31.  
  32.     if(Sttylink != -1){
  33.         return 0;
  34.     }
  35.     psignal(Curproc,0);    /* Don't keep the parser waiting */
  36.     chname(Curproc,"TTYlink listener");
  37.  
  38.     lsocket.sin_family = AF_INET;
  39.     lsocket.sin_addr.s_addr = INADDR_ANY;
  40.     if(argc < 2)
  41.         lsocket.sin_port = IPPORT_TTYLINK;
  42.     else
  43.         lsocket.sin_port = atoi(argv[1]);
  44.  
  45.     Sttylink = socket(AF_INET,SOCK_STREAM,0);
  46.     bind(Sttylink,(char *)&lsocket,sizeof(lsocket));
  47.     listen(Sttylink,1);
  48.     for(;;){
  49.         if((s = accept(Sttylink,NULLCHAR,(int *)NULL)) == -1)
  50.             break;    /* Service is shutting down */
  51.         
  52.         if(availmem() < Memthresh){
  53. #ifndef TNOS_68K
  54.             usprintf(s,"System is overloaded; try again later\r\n");
  55. #else
  56.             usprintf(s,"System is overloaded; try again later\r\l");
  57. #endif
  58.             shutdown(s,1);
  59.         } else if(!Attended)    {
  60.             usprintf(s,SysopBusy);
  61.             shutdown(s,1);
  62.         } else {
  63.             type = TELNET;
  64.             newproc("chat",2048,ttylhandle,s,(void *)&type,NULL,0);
  65.         }
  66.     }
  67.     return 0;
  68. }
  69. /* This function handles all incoming "chat" sessions, be they TCP,
  70.  * NET/ROM or AX.25
  71.  */
  72. void
  73. ttylhandle(s,t,p)
  74. int s;
  75. void *t;
  76. void *p;
  77. {
  78. int type,index;
  79. struct session *sp;
  80. char addr[MAXSOCKSIZE];
  81. int len = MAXSOCKSIZE;
  82. struct telnet tn;
  83. extern char *Motd;
  84. time_t nowtime;
  85.  
  86.     type = * (int *)t;
  87.     sockmode(s,SOCK_ASCII);
  88.     sockowner(s,Curproc);    /* We own it now */
  89.     log(s,"open %s",Sestypes[type]);
  90.  
  91.     /* Allocate a session descriptor */
  92.     if((sp = newsession(NULLCHAR,type,1)) == NULLSESSION){
  93.         tputs(TooManySessions);
  94.         close_s(s);
  95.         return;
  96.     }
  97.     index = sp - Sessions;
  98.  
  99.     /* Initialize a Telnet protocol descriptor */
  100.     memset((char *)&tn,0,sizeof(tn));
  101.     tn.session = sp;    /* Upward pointer */
  102.     sp->cb.telnet = &tn;    /* Downward pointer */
  103.     sp->s = s;
  104.     sp->proc = Curproc;
  105.  
  106.     time(&nowtime);        /* current time */
  107.  
  108.     getpeername(s,addr,&len);
  109.     tprintf("\007Incoming %s session %u from %s on %s",
  110.      Sestypes[type],index,psocket(addr),ctime(&nowtime));
  111.  
  112.     usprintf(s, Tnbanner, Hostname, Attended ? "attended" : "unattended");
  113.     if(Motd != NULLCHAR)
  114.         usprintf(s, "%s", Motd);
  115.  
  116.     tnrecv(&tn);
  117. }
  118.  
  119. /* Shut down Ttylink server */
  120. int
  121. ttyl0(argc,argv,p)
  122. int argc;
  123. char *argv[];
  124. void *p;
  125. {
  126.     return (deleteserver (&Sttylink));
  127. }
  128. #endif /*ALLSERV*/
  129.